﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Publish_Shop : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string state = "", location = "";
            if (Request.Params["state"] != null)
                state = Request.Params["state"].ToString();
            if (Request.Params["location"] != null)
                location = Request.Params["location"].ToString();
            int i = 0;
            for (i = 0; i < ddlState.Items.Count; i++)
                if (ddlState.Items[i].Value == state)
                {
                    ddlState.SelectedIndex = i;
                    cddState.SelectedValue = state;
                    break;
                }
            for (i = 0; i < ddlLocation.Items.Count; i++)
                if (ddlLocation.Items[i].Value == location)
                {
                    ddlLocation.SelectedIndex = i;
                    cddLocation.SelectedValue = location;
                    break;
                }
            lstShop.DataSource = GetListOfShops(state, location);
            lstShop.DataBind();
        }
    }

    protected string BindShop(string id, string title, string image)
    {
        string rs = "";
        if (image != "")
        {
            rs += "<table align='center'><tr><td style='padding: 4px;'><a href='ShopDetail.aspx?id=" + id + "'>";
            rs += "<img alt='' src='ShopThumbnail.aspx?id=" + id + "&width=150' border='0' align='left' onerror=\"this.src='../images/noimage90.gif'\" />";
            rs += "</a></td></tr></table>";
        }
        rs += "<p class='title'><a href='ShopDetail.aspx?id=" + id + "'>" + title + "</a></p>";
        rs += "<p>" + title + "</p>";
        return rs;
    }

    protected void btnView_ServerClick(object sender, EventArgs e)
    {
        string stateID = "", locationID = "";
        stateID = ddlState.SelectedValue;
        locationID = ddlLocation.SelectedValue;
        if (stateID == "" && locationID == "")
        {
            HttpContext.Current.Cache.Remove("IDState");
            HttpContext.Current.Cache.Remove("IDLocation");
            Response.Redirect(@"Shop.aspx", false);
        }
        else
        {
            if (HttpContext.Current.Cache["IDState"] != null)
                HttpContext.Current.Cache.Remove("IDState");
            HttpContext.Current.Cache.Insert("IDState", stateID);

            if (HttpContext.Current.Cache["IDLocation"] != null)
                HttpContext.Current.Cache.Remove("IDLocation");
            HttpContext.Current.Cache.Insert("IDLocation", locationID);
            Response.Redirect(@"Shop.aspx?state=" + stateID + "&location=" + locationID, false);
        }
    }

    private DataTable GetListOfShops(string stateID, string locationID)
    {
        DataTable dt = null;
        try
        {
            GssBL.BLShop BL = new GssBL.BLShop();
            dt = BL.getShopByArea(stateID, locationID);
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            dt = null;
        }
        return dt;
    }
}
